package edu.unlv.cs.ladders.entities;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import edu.unlv.cs.ladders.entities.BaseEntity;

import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.entity.RooEntity;
import javax.validation.constraints.NotNull;
import edu.unlv.cs.ladders.entities.Participant;

@Entity
@RooJavaBean
@RooEntity(finders = { "findTeamsByTournament" })
public class Team extends BaseEntity {
	private static final long serialVersionUID = 1L;

	@NotNull
    private String name;

    @NotNull
    private String code;

    private Boolean hasPaid = Boolean.FALSE;

    private Integer currentStanding;

    @ManyToOne
    private Participant captain;

    @ManyToMany
    private Set<Participant> members;

    @NotNull
    @ManyToOne
    private Tournament tournament;

    @OneToMany
    private Set<Score> scores;
    
    @OneToMany
    private Set<Event> events;
    
    private Integer rank = 0;
    
    public String toString(){
    	return this.name;
    }
    public void addMember(Participant member){
    	if(!getMembers().contains(member)){
    		getMembers().add(member);
    		this.merge();
    	}
    	
    }
    public void addEvent(Event event){
    	if(!getEvents().contains(event)){
    		getEvents().add(event);
    		this.merge();
    	}
    }
    public void addScore(Score score){
    	if(!getScores().contains(score)){
    		getScores().add(score);
    		this.merge();
    	}
    }
    public Set<Participant> getMembers(){
    	if(this.members==null){
    		this.members = new HashSet<Participant>();
    	}
    	return this.members;
    }
    public Set<Score> getScores(){
    	if(this.scores==null){
    		this.scores = new HashSet<Score>();
    	}
    	return this.scores;
    }
    public Set<Event> getEvents(){
    	if(this.events==null){
    		this.events = new HashSet<Event>();
    	}
    	return this.events;
    }
    @Transient
    public boolean canChallenge(Participant participant){
    	boolean canChallenge = false;
    	Team challenger = tournament.findTeamWithMember(participant);
    	
    	if( challenger!=null &&
    		challenger.getRank()>getRank() &&
    	   !hasMember(participant)){
    		canChallenge = true;
    	}
    	return canChallenge;
    }
    @Transient
    public boolean hasMember(Participant participant){
    	if(participant==null){
    		return false;
    	}
    	return this.getMembers().contains(participant);
    }
    @Transient
    public boolean canJoin(Participant participant){
    	boolean isMember = false;
    	if(participant==null){
    		return false;
    	}
    	Collection<Team> teams = getTournament().getTeams();
    	for(Team team : teams){
    		isMember = isMember || team.hasMember(participant);
    	}
    	return !isMember;
    }
    @Transient
    public boolean isWinner(Event event){
    	if(this.equals(event.getWinner())){
    		return true;
    	}
    	return false;
    }
}
